import { request } from "@/utils/service"
import type * as Task from "./types/task"
import type * as StaticTask from "./types/static-task"
import { AxiosResponse } from "axios"

/** 增 */
export function createTaskDataApi(data: Task.CreateOrUpdateTaskData | StaticTask.CreateOrUpdateTaskData) {
  return request<Task.TaskSaveResponseData>({
    url: "/toksim/training/task/create",
    method: "post",
    data
  })
}

/** 删 */
export function deleteTaskDataApi(id: string) {
  return request({
    url: `/toksim/training/task/delete/${id}`,
    method: "post"
  })
}

/** 改 */
export function updateTaskDataApi(data: Task.CreateOrUpdateTaskData | StaticTask.CreateOrUpdateTaskData) {
  return request({
    url: "/toksim/training/task/edit",
    method: "post",
    data
  })
}

/** 查 */
export function getTableDataApi(params: Task.PageTableRequestData) {
  return request<Task.SearchTableResponseData>({
    url: "/toksim/training/task/search",
    method: "post",
    data: params
  })
}

/** 根据id查询任务信息 */
export function getTaskInfoApi(id: string) {
  return request<Task.TaskInfoResponseData>({
    url: `/toksim/training/task/get/${id}`,
    method: "get"
  })
}

/** 启动任务 */
export function startTaskApi(id: string) {
  return request({
    url: `/toksim/training/task/start/${id}`,
    method: "post"
  })
}

/** 暂停任务 */
export function pauseTaskApi(id: string) {
  return request({
    url: `/toksim/training/task/stop/${id}`,
    method: "post"
  })
}

/** 结束任务 */
export function stopTaskApi(id: string) {
  return request({
    url: `/toksim/training/task/end/${id}`,
    method: "post"
  })
}

/** 获取任务过程数据 */
export function getTaskProcessDataApi(id: string) {
  return request<Task.TaskProcessDataResponseData>({
    url: `/toksim/training/task/data/get`,
    method: "post",
    data: {
      task_id: id
    }
  })
}

/** 获取任务过程文件 */
export function getTaskProcessFileApi(id: string, type: Task.TaskProcessFileType) {
  return request<Task.TaskProcessFileResponseData>({
    url: `/toksim/resource/process/${id}/${type}/list`,
    method: "get"
  })
}

/** 删除任务过程文件 */
export function deleteProcessFileApi(id: string, file_id: string) {
  return request<Task.TaskProcessFileResponseData>({
    url: `/toksim/resource/process/${id}/delete/${file_id}`,
    method: "post"
  })
}

/** 下载任务过程文件 */
export function downloadProcessFileApi(id: string, file_id: string) {
  return request<AxiosResponse>({
    headers: {
      "Content-Type": "application/octet-stream"
    },
    responseType: "blob",
    url: `/toksim/resource/process/${id}/download/${file_id}`,
    method: "get"
  })
}
